package ru.alt22.visor.php.ui.pages;
//TODO Check for parent Source folders (disable control if one of parent folder is "source folder")

import org.eclipse.core.resources.IResource;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPropertyPage;
import org.eclipse.ui.dialogs.PropertyPage;

import ru.alt22.visor.php.properties.Properties;

public class FolderPropertyPage extends PropertyPage implements IWorkbenchPropertyPage {
	private static final String PATH_TITLE = "Path:";
	private static final String SOURCE_FOLDER_TITLE = "&Source folder";
	
	private Button sourceFolder;

	public FolderPropertyPage() {
		super();
	}

	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		GridData data = new GridData(GridData.FILL);
		data.grabExcessHorizontalSpace = true;
		composite.setLayoutData(data);
		
		createInformationControls(composite);
		createSepartor(composite);
		createPropertiesControls(composite);
		
		readValues();

		return composite;
	}

	private void createInformationControls(Composite parent) {
		Composite composite = createInformationComposite(parent);
		
		Label pathLabel = new Label(composite, SWT.NONE);
		pathLabel.setText(PATH_TITLE);

		Text pathValueText = new Text(composite, SWT.WRAP | SWT.READ_ONLY);
		pathValueText.setText(((IResource) getElement()).getFullPath().toString());
	}

	private Composite createInformationComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);

		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		composite.setLayoutData(data);

		return composite;
	}

	private void createSepartor(Composite composite) {
		Label separator = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		separator.setLayoutData(gridData);
	}

	private void createPropertiesControls(Composite composite) {
		sourceFolder = new Button(composite, SWT.CHECK);
		sourceFolder.setText(SOURCE_FOLDER_TITLE);
	}
	
	private void readValues() {
		String value = Properties.getPropertyValue((IResource) getElement(), Properties.SOURCE_FOLDER_PROPERTY);
		sourceFolder.setSelection((value != null) ? Properties.stringToBoolean(value).booleanValue() : Properties.SOURCE_FOLDER_DEFAULT);
	}

	protected void performDefaults() {
		sourceFolder.setSelection(Properties.SOURCE_FOLDER_DEFAULT);
	}
	
	public boolean performOk() {
		return Properties.setPropertyValue(
			(IResource) getElement(),
			Properties.SOURCE_FOLDER_PROPERTY,
			Properties.boolToString(sourceFolder.getSelection())
		);
	}

}